package com.hhh.petadopt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhh.petadopt.pojo.Admin;
import com.hhh.petadopt.service.adminService;
import com.hhh.petadopt.utils.JsonUtils;

/**
 * 管理员管理
 * @author 杜镇声声
 *
 */
@Controller
public class adminController {
	@Autowired
	private adminService adminservice;
	
	//查询所有管理员
	@RequestMapping(value="/admin/admin.html")
	public String adminList(Model model) {
		List<Admin> adminList = adminservice.adminLists();
		//将所有管理员信息保存到域中
		model.addAttribute("adminList", adminList);
		//跳转到管理管理员页面
		return "admin/admin";
	}
	
	//管理员详细信息回显
	@RequestMapping(value="/admin/adminInfo.html")
	@ResponseBody
	public String selectAdminByid(@Param("id")Integer id) {
		Admin adminInfo = adminservice.selectAdminById(id);
		String objectToJson = JsonUtils.objectToJson(adminInfo);
		return objectToJson;
	}
	
	//根据管理员id修改管理员信息
	@RequestMapping(value="/admin/updateadminByid.html")
	@ResponseBody
	public String updateAdminByid(Admin admin) {
		adminservice.updateAdminByIds(admin);
		return "ok";
	}
	
	//添加管理员
	@RequestMapping(value="/admin/addadmin.html")
	@ResponseBody
	public String addAdmin(Admin admin) {
		//添加操作
		adminservice.addAdmins(admin);
		return "ok";
	}
	
	//删除管理员
	@RequestMapping(value="/admin/deleteAdmin.html")
	@ResponseBody
	public String deleteAdminByid(Integer id) {
		//删除操作
		adminservice.deleteAdmin(id);
		return "ok";
	}
	//批量删除管理员
	@RequestMapping(value="/admin/deleteadmins.html")
	@ResponseBody
	public String deleteAdmins(Integer[] ids) {
		//删除操作
		adminservice.deleteAdminsByIds(ids);
		return "ok";
	}
	//搜索用户
	@RequestMapping(value="/admin/serachAdmin.html")
	public String searChAdmin(@Param(value="word")String word,HttpServletRequest request) {
		System.out.println(word);
		//执行查询
		List<Admin> adminList = adminservice.searchAdminByWord(word);
		//搜索条件回显
		request.setAttribute("word", word);
		//将数据存储到request域中
		request.setAttribute("adminList", adminList);
		//返回视图
		return "admin/admin";
	}

}
